import axios from "axios"
export const file =  {

  async uploadImage(type,file,maxSize=5){
     let imageSize=file?.size/1024/1024
  
    if(imageSize>maxSize)  return {code:1,message:`上传图片大小不能超过 ${maxSize} MB!`}
    const whiteList = ["image/jpeg", "image/png"];
    if (whiteList.indexOf(file.type) === -1) 
        return {code:1,message:'上传文件类型只能是 image/jpeg, image/png'}

    var formData = new FormData();
    formData.append(`file`,file);
     try{
        let res = await  axios({
            method: 'POST',
            url: `/api/File/upload/image/${type}`,
            headers: {
                'Content-Type':'multpart/form-data',
                'token':localStorage.getItem('token')
            },
            data: formData,
        })
        return res.data
    }catch(error){return{code:400,message:error}}
 }
}